#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Osh.Teletext
{
	public class PageHeaderPacket : TeletextPacket
	{
		private PageAddress _pageAddress;
		private byte[] _data;
		private bool _erasePage;
		private bool _newsflash;
		private bool _subtitle;
		private bool _suppressHeader;
		private bool _updateIndicator;
		private bool _interruptedSequence;
		private bool _inhibitDisplay;
		private bool _magazineSerial;
		private int _nationalCharacterSubset;
		
		public PageHeaderPacket(int magazine, byte[] data, int position) : base(magazine, 0)
		{
			_pageAddress = TeletextDecoder.DecodePageAddress(data, position, magazine);

			_erasePage = (data[position + 3] & 0x80) != 0;	// C4 - Byte 9, bit 8
			_newsflash = (data[position + 5] & 0x20) != 0;	// C5 - Byte 11, bit 6
			_subtitle = (data[position + 5] & 0x80) != 0;	// C6 - Byte 11, bit 8
			_suppressHeader = (data[position + 6] & 0x02) != 0;	// C7 - Byte 12, bit 2
			_updateIndicator = (data[position + 6] & 0x08) != 0;	// C8 - Byte 12, bit 4
			_interruptedSequence = (data[position + 6] & 0x20) != 0;	// C9 - Byte 12, bit 6
			_inhibitDisplay = (data[position + 6] & 0x80) != 0;	// C10 - Byte 12, bit 8
			_magazineSerial = (data[position + 7] & 0x02) != 0;	// C11 - Byte 13, bit 2
			
			byte byte13 = (byte)(HummingDecoder.Decode(data[position + 7]) >> 1);	// C14, C13, C12
			_nationalCharacterSubset = ((byte13 & 1) << 2) | (byte13 & 2) | (byte13 >> 2);	// C12, C13, C14 (in reversed order)

			_data = new byte[32];
			Buffer.BlockCopy(data, position + 8, _data, 0, 32);
		}

		public PageAddress PageAddress
		{
			get { return _pageAddress; }
		}

		/// <summary>
		/// Packets X/1 to X/28 belonging to a previous transmission of the page should be
		/// erased from the decoder's memory before packets belonging to the associated page
		/// are stored.
		/// </summary>
		public bool ErasePage
		{
			get { return _erasePage; }
		}

		/// <summary>
		/// When set to true this property indicates that the associated page is a Newsflash page.
		/// All information intended for display on such a page will be boxed and will be displayed
		/// inset into the normal video picture.
		/// </summary>
		public bool Newsflash
		{
			get { return _newsflash; }
		}

		/// <summary>
		/// When set to true this property indicates that the associated page is a subtitle page.
		/// All information intended for display on such a page will be boxed and will be displayed
		/// inset into the normal video picture.
		/// </summary>
		public bool Subtitle
		{
			get { return _subtitle; }
		}

		/// <summary>
		/// Data addressed to row 0 is not to be displayed.
		/// </summary>
		public bool SuppressHeader
		{
			get { return _suppressHeader; }
		}

		/// <summary>
		/// Data within packets X/1 to X/28 of the associated page has been changed since the
		/// previous transmission. The setting of this bit is under editorial control.
		/// </summary>
		public bool UpdateIndicator
		{
			get { return _updateIndicator; }
		}

		/// <summary>
		/// The associated page is not in numerical order of page sequence, allowing the header
		/// to be excluded from a rolling header display to avoid discontinuities.
		/// </summary>
		public bool InterruptedSequence
		{
			get { return _interruptedSequence; }
		}

		/// <summary>
		/// Data addressed to rows 1 to 24 is not to be displayed.
		/// </summary>
		public bool InhibitDisplay
		{
			get { return _inhibitDisplay; }
		}

		/// <summary>
		/// When set to true the service is designated to be in Serial mode and the transmission of
		/// a page is terminated by the next page header with a different page number.
		/// When set to false the service is designated to be in Parallel mode and the transmission
		/// of a page is terminated by the next page header with a different page number but the
		/// same magazine number.
		/// The same setting shall be used for all page headers in the service.
		/// </summary>
		public bool MagazineSerial
		{
			get { return _magazineSerial; }
		}

		/// <summary>
		/// Where the decoder is capable of displaying text in more than one language these
		/// property is used to select G0 character set options, (see clause 15.2).
		/// The response to these property may be modified by packets X/28/0 Format 1, X/28/4, M/29/0 and M/29/4.
		/// </summary>
		public int NationalCharacterSubset
		{
			get { return _nationalCharacterSubset; }
		}

		public byte[] Data
		{
			get { return _data; }
		}
	}
}
